import 'dart:convert';
import 'package:ecy_app_flutter/ecycommon/EcyAPI.dart';
import 'package:ecy_app_flutter/ecycommon/EcyRequestBaseManager.dart';
import 'package:http/http.dart' as http;
import '../models/exchange_card.dart';
import '../models/exchange_request.dart';
import '../models/exchange_user.dart';

class ExchangeService {
  static const String baseUrl = '/app'; // 替换为实际的API基础URL
  static const String sellerId = '1234567890'; // 替换为实际的卖家ID
  // 检查用户是否已购买交换功能
  Future<bool> checkExchangePermission() async {
    try {
      final response = await EcyRequestbasemanager().get(EcyAPI.shop.url + "/$sellerId", {});
      if (response['code'] == 0 || response['code'] == 200) {
        final data = response['result'];
        return data['hasPermission'] as bool;
      }
      return false;
    } catch (e) {
      print('Error checking exchange permission: $e');
      return false;
    }
  }

  // 获取互换集市中的卡片列表
  Future<List<ExchangeCard>> getMarketCards() async {
    try {
      final response = await http.get(Uri.parse('$baseUrl/market/cards'));
      if (response.statusCode == 200) {
        final List<dynamic> data = json.decode(response.body);
        return data.map((json) => ExchangeCard.fromJson(json)).toList();
      }
      return [];
    } catch (e) {
      print('Error getting market cards: $e');
      return [];
    }
  }

  // 获取我的交换卡片列表
  Future<List<ExchangeCard>> getMyExchangeCards() async {
    try {
      final response = await http.get(Uri.parse('$baseUrl/my/cards'));
      if (response.statusCode == 200) {
        final List<dynamic> data = json.decode(response.body);
        return data.map((json) => ExchangeCard.fromJson(json)).toList();
      }
      return [];
    } catch (e) {
      print('Error getting my exchange cards: $e');
      return [];
    }
  }

  // 获取卡片的交换申请列表
  Future<List<ExchangeRequest>> getCardExchangeRequests(String cardId) async {
    try {
      final response = await http.get(Uri.parse('$baseUrl/cards/$cardId/requests'));
      if (response.statusCode == 200) {
        final List<dynamic> data = json.decode(response.body);
        return data.map((json) => ExchangeRequest.fromJson(json)).toList();
      }
      return [];
    } catch (e) {
      print('Error getting card exchange requests: $e');
      return [];
    }
  }

  // 申请交换卡片
  Future<bool> applyExchange({
    required String requestedCardId,
    required String offeredCardId,
  }) async {
    try {
      final response = await http.post(
        Uri.parse('$baseUrl/apply'),
        body: json.encode({
          'requestedCardId': requestedCardId,
          'offeredCardId': offeredCardId,
        }),
        headers: {'Content-Type': 'application/json'},
      );
      return response.statusCode == 200;
    } catch (e) {
      print('Error applying exchange: $e');
      return false;
    }
  }

  // 处理交换申请（接受或拒绝）
  Future<bool> handleExchangeRequest({
    required String requestId,
    required bool accept,
  }) async {
    try {
      final response = await http.post(
        Uri.parse('$baseUrl/requests/$requestId/${accept ? 'accept' : 'reject'}'),
        headers: {'Content-Type': 'application/json'},
      );
      return response.statusCode == 200;
    } catch (e) {
      print('Error handling exchange request: $e');
      return false;
    }
  }

  getUserGroups(param0) {}
} 